package server.core;

/**
 * describe:糖果机
 *
 * @author helloworldyu
 * @data 2018/3/23
 */
public class GumballMachine {
	public static final int NO_GUMBALL = 0;

	NoQuarterState noQuarterState = new NoQuarterState(this);
	HasQuarterState hasQuarterState = new HasQuarterState(this);
	SoldingState soldingState = new SoldingState(this);
	WinnerState winnerState = new WinnerState(this);
	SoldOutState soldOutState = new SoldOutState(this);

	//当前的状态
	IState state = noQuarterState;
	//当前的个数
	int count = NO_GUMBALL;

	public GumballMachine(int count) {
		if( NO_GUMBALL >= count ){
			this.count = 0;
		}else {
			this.count = count;
		}
	}

	public int getCount() {
		return count;
	}


	/**
	 * 放入一个硬币
	 */
	public void insertQuarter(){
		state.insertQuarter();
	}

	/**
	 * 取回硬币
	 */
	public void ejectQuarter(){
		state.ejectQuarter();
	}

	/**
	 * 转动手柄
	 */
	public void turnCrank(){
		state.turnCrank();
		state.dispense();
	}

	public void releaseBall(){
		System.out.println("给出一个糖果");
		if( this.count != 0 ){
			count--;
		}
	}

	public HasQuarterState getHasQuarterState() {
		return hasQuarterState;
	}

	public void setHasQuarterState(HasQuarterState hasQuarterState) {
		this.hasQuarterState = hasQuarterState;
	}

	public NoQuarterState getNoQuarterState() {
		return noQuarterState;
	}

	public void setNoQuarterState(NoQuarterState noQuarterState) {
		this.noQuarterState = noQuarterState;
	}

	public SoldingState getSoldingState() {
		return soldingState;
	}

	public void setSoldingState(SoldingState soldingState) {
		this.soldingState = soldingState;
	}

	public WinnerState getWinnerState() {
		return winnerState;
	}

	public void setWinnerState(WinnerState winnerState) {
		this.winnerState = winnerState;
	}

	public SoldOutState getSoldOutState() {
		return soldOutState;
	}

	public void setSoldOutState(SoldOutState soldOutState) {
		this.soldOutState = soldOutState;
	}

	public IState getState() {
		return state;
	}

	public void setState(IState state) {
		this.state = state;
	}


	@Override
	public String toString() {
		return "GumballMachine{" +
				"noQuarterState=" + noQuarterState +
				", hasQuarterState=" + hasQuarterState +
				", soldingState=" + soldingState +
				", winnerState=" + winnerState +
				", soldOutState=" + soldOutState +
				", state=" + state +
				", count=" + count +
				'}';
	}
}